package main

import "strings"
import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"qq_service/find_qq_by_qun/protobuf/pb"
)

var Addresses = []string{"127.0.0.1:9003"}
var i int = 0

func ServerMsg(msg string) {

	//收到消息，拆解
	//mysend.SendData(qun+"$#$"+resp.Word+"$#$"+resp.Email)
	msglist := strings.Split(msg, "$#$")
	qunnum := msglist[0]
	word := msglist[1]
	email := msglist[2]

	//顺序轮询负载均衡
	conn, err := grpc.Dial(Addresses[i%len(Addresses)], grpc.WithInsecure())
	if err != nil {
		fmt.Println(err)
	}
	defer conn.Close()

	c := pb.NewFindQqByQunServiceClient(conn)

	req := &pb.QqRequest{QunNum: qunnum, Word: word, Email: email}
	res, err := c.FindQqByQun(context.Background(), req)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("收到", len(res.Qq))
	i++

}

func main() {
	rr := NewReceivedRabbitMQ("amqp://admin:admin@192.168.56.103:5672/", "FindQQbyQun_task_queue")
	rr.ReceiveData(ServerMsg)
}
